package com.member.web.controller.shop;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.member.common.annotation.Log;
import com.member.common.core.controller.BaseController;
import com.member.common.core.domain.AjaxResult;
import com.member.common.core.page.TableDataInfo;
import com.member.common.enums.BusinessType;
import com.member.common.utils.poi.ExcelUtil;
import com.member.shop.domain.SysBizGoodsSku;
import com.member.shop.domain.SysBizRechargeUser;
import com.member.shop.domain.SysBizRecommendCategory;
import com.member.shop.domain.SysBizRecommendGoods;
import com.member.shop.service.ISysBizRecommendCategoryService;
import com.member.shop.service.ISysBizRecommendGoodsService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 【推荐分类】控制器
 *
 * @author lixinchen
 * @version 1.0
 * @date 2025-01-08
 **/
@RestController
@RequestMapping("/shop/recommendCategory")
public class SysBizRecommendCategoryController extends BaseController {
    @Resource
    private ISysBizRecommendCategoryService sysBizRecommendCategoryService;

    @Resource
    private ISysBizRecommendGoodsService sysBizRecommendGoodsService;

    /**
     * 查询推荐分类列表
     */
    @PreAuthorize("@ss.hasPermi('shop:recommendCategory:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysBizRecommendCategory sysBizRecommendCategory) {
        startPage();
        setTenantId(sysBizRecommendCategory);
        List<SysBizRecommendCategory> list = sysBizRecommendCategoryService.selectSysBizRecommendCategoryList(sysBizRecommendCategory);
        return getDataTable(list);
    }

    @GetMapping(value = "/listGoodsBind/{id}")
    public TableDataInfo listGoodsBind(@PathVariable("id") Long id) {
        List<SysBizGoodsSku> list = sysBizRecommendCategoryService.listGoodsBind(id);
        return getDataTable(list);
    }

    private void setTenantId(SysBizRecommendCategory sysBizRecommendCategory) {
        Long tenantId = getTenantId();
        if (tenantId != null && tenantId > 0) {
            sysBizRecommendCategory.setTenantId(tenantId);
        }
    }

    /**
     * 导出推荐分类列表
     */
    @PreAuthorize("@ss.hasPermi('shop:recommendCategory:export')")
    @Log(title = "推荐分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysBizRecommendCategory sysBizRecommendCategory) {
        setTenantId(sysBizRecommendCategory);
        List<SysBizRecommendCategory> list = sysBizRecommendCategoryService.selectSysBizRecommendCategoryList(sysBizRecommendCategory);
        ExcelUtil<SysBizRecommendCategory> util = new ExcelUtil<SysBizRecommendCategory>(SysBizRecommendCategory.class);
        util.exportExcel(response, list, "推荐分类数据");
    }

    /**
     * 获取推荐分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('shop:recommendCategory:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(sysBizRecommendCategoryService.selectSysBizRecommendCategoryById(id));
    }

    /**
     * 新增推荐分类
     */
    @PreAuthorize("@ss.hasPermi('shop:recommendCategory:add')")
    @Log(title = "推荐分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysBizRecommendCategory sysBizRecommendCategory) {
        List<SysBizRecommendCategory> sysBizRecommendCategories = sysBizRecommendCategoryService.selectSysBizRecommendCategoryList(
                new LambdaQueryWrapper<SysBizRecommendCategory>()
                        .eq(SysBizRecommendCategory::getTenantId, sysBizRecommendCategory.getTenantId())
                        .eq(SysBizRecommendCategory::getDeleted, 0)
        );
        if (!CollectionUtils.isEmpty(sysBizRecommendCategories)) {
            if (sysBizRecommendCategories.size() >= 4) {
                return error("一个站点最多有4个推荐分类");
            }

            long countByName = sysBizRecommendCategories.stream()
                    .filter(category -> category.getName().equals(sysBizRecommendCategory.getName()))
                    .count();

            if (countByName >= 1) {
                return error("站点中推荐分类名称不能重复");
            }
        }

        return toAjax(sysBizRecommendCategoryService.insertSysBizRecommendCategory(sysBizRecommendCategory));
    }

    /**
     * 修改推荐分类
     */
    @PreAuthorize("@ss.hasPermi('shop:recommendCategory:edit')")
    @Log(title = "推荐分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysBizRecommendCategory sysBizRecommendCategory) {
        List<SysBizRecommendCategory> sysBizRecommendCategories = sysBizRecommendCategoryService.selectSysBizRecommendCategoryList(
                new LambdaQueryWrapper<SysBizRecommendCategory>()
                        .eq(SysBizRecommendCategory::getTenantId, sysBizRecommendCategory.getTenantId())
                        .ne(SysBizRecommendCategory::getId, sysBizRecommendCategory.getId())
                        .eq(SysBizRecommendCategory::getDeleted, 0)
        );
        if (!CollectionUtils.isEmpty(sysBizRecommendCategories)) {
            if (sysBizRecommendCategories.size() >= 4) {
                return error("一个站点最多有4个推荐分类");
            }

            long countByName = sysBizRecommendCategories.stream()
                    .filter(category -> category.getName().equals(sysBizRecommendCategory.getName()))
                    .count();

            if (countByName >= 1) {
                return error("站点中推荐分类名称不能重复");
            }
        }
        return toAjax(sysBizRecommendCategoryService.updateSysBizRecommendCategory(sysBizRecommendCategory));
    }

    /**
     * 删除推荐分类
     */
    @PreAuthorize("@ss.hasPermi('shop:recommendCategory:remove')")
    @Log(title = "推荐分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sysBizRecommendCategoryService.deleteSysBizRecommendCategoryByIds(ids));
    }
}
